package com.have.blog.admin.service.impl;

import com.have.blog.admin.constant.AdminBlogConstant;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.domain.dto.WebConfigDto;
import com.have.blog.admin.service.SystemConfigService;
import com.have.blog.admin.service.WebConfigService;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.WebConfigMapper;
import com.have.blog.model.models.WebConfig;
import com.have.blog.model.models.WebConfigExample;
import com.have.blog.picture.service.PictureFeignApi;
import com.have.blog.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class WebConfigServiceImpl implements WebConfigService {
    @Autowired
    private WebConfigMapper webConfigMapper;
    @Autowired
    private PictureFeignApi pictureFeignApi;
    @Autowired
    private SystemConfigService systemConfigService;


    @Override
    public Wrapper<WebConfigDto> getWebConfig() {
        WebConfigExample example = new WebConfigExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("create_time desc");
        List<WebConfig> webConfigs = webConfigMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(webConfigs)) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_NOT_EXIST);
        }
        WebConfig webConfig = webConfigs.get(0);
        System.out.println(webConfig);
        WebConfigDto webConfigDto = MyBeanUtils.copyProperties(webConfig, WebConfigDto::new);
        System.out.println(webConfigDto);
        //获取图片
        Wrapper<String> picWrapper;
        if (webConfigDto.getLogoUid() != null) {
            picWrapper = pictureFeignApi.getPictureUrl(webConfigDto.getLogoUid());
            if (picWrapper.isSuccess()) {
                String picUrl = picWrapper.getData();
                webConfigDto.setPhotoUrl(picUrl);
            }

        }

        //获取支付宝收款二维码
        if (webConfigDto.getAliPayPictureUid() != null) {
            picWrapper = pictureFeignApi.getPictureUrl(webConfigDto.getAliPayPictureUid());
            if (picWrapper.isSuccess()) {
                String aliUrl = picWrapper.getData();
                webConfigDto.setAliPayPhotoUrl(aliUrl);
            }
        }
        //获取微信收款二维码
        if (webConfigDto.getWeixinPayPictureUid() != null) {
            picWrapper = this.pictureFeignApi.getPictureUrl(webConfigDto.getWeixinPayPictureUid());
            if (picWrapper.isSuccess()) {
                String weixiUrl = picWrapper.getData();
                webConfigDto.setWeixinPayPhotoUrl(weixiUrl);
            }
        }
        return Wrapper.success(webConfigDto);

    }


    @Override
    public Wrapper getWebSiteName() {
        WebConfigExample example = new WebConfigExample();
        List<WebConfig> WebConfigs = webConfigMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(WebConfigs)) {
            return Wrapper.success(WebConfigs.get(0).getWebName());
        }
        return Wrapper.success("");
    }



    @Override
    public Wrapper editWebConfig(WebConfigDto webConfigDto) {
        int result = 0;
        if (webConfigDto.getUid() == null) {
            WebConfig webConfig = new WebConfig();
            // 设置网站配置【使用Spring工具类提供的深拷贝】
            MyBeanUtils.copyProperties(webConfigDto, webConfig);
            webConfig.setStatus(BlogStatus.ENABLE.getStatus());
            result = webConfigMapper.insert(webConfig);
        } else {
            WebConfig webConfig = webConfigMapper.selectByPrimaryKey(webConfigDto.getUid());
            // 更新网站配置【使用Spring工具类提供的深拷贝】
            MyBeanUtils.copyProperties(webConfigDto, webConfig, AdminBlogConstant.STATUS, AdminBlogConstant.UID);
            result = webConfigMapper.updateByPrimaryKeySelective(webConfig);
            // TODO 修改配置后，清空Redis中的 WEB_CONFIG
            // 同时清空Redis中的登录方式
        }

        if (result <= 0) {
            return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
        }
        return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
    }

    @Override
    public Boolean isOpenLoginType(String loginType) {
        return null;
    }
}
